﻿// -----------------------------------------------------------------------
// <copyright file="Image.cs" company="Microsoft">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace AwesomeUtils
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;

    /// <summary>
    /// TODO: Update summary.
    /// </summary>
    public class Image
    {

        public static System.Drawing.Image SizeImage(System.Drawing.Image image, int width)
        {
            System.Drawing.Image sizedImage = image;
            if (width != image.Width)
            {
                try
                {
                    //Size the image
                    int oldW = image.Width;
                    int oldH = image.Height;
                    double percent = ((double)width / (double)oldW);
                    int w = (int)((double)oldW * percent);
                    int h = (int)((double)oldH * percent);
                    System.IntPtr val = new IntPtr(0);
                    sizedImage = new Bitmap(w, h, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                    Graphics g = Graphics.FromImage(sizedImage);
                    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
                    g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
                    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
                    g.DrawImage(image, 0, 0, w, h);
                }

                catch (Exception)
                {
                    sizedImage = image;
                }
            }

            return sizedImage;
        }

        public static void SaveImage(System.Drawing.Image image, string filename)
        {
            //can read anything
            try
            {
                //Create the directory if it doesn't already exist
                int lastSlash = filename.LastIndexOf("\\");
                if (lastSlash != -1)
                {
                    string dir = filename.Substring(0, lastSlash);
                    if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
                }

                ImageFormat imgFormat = null;
                switch (Path.GetExtension(filename.ToLower()))
                {
                    case ".jpeg": imgFormat = ImageFormat.Jpeg; break;
                    case ".jpg": imgFormat = ImageFormat.Jpeg; break;
                    case ".gif": imgFormat = ImageFormat.Gif; break;
                    case ".png": imgFormat = ImageFormat.Png; break;
                    case ".tif": imgFormat = ImageFormat.Tiff; break;
                    case ".bmp": imgFormat = ImageFormat.Bmp; break;
                    case ".ico": imgFormat = ImageFormat.Icon; break;
                }
                if (imgFormat != null) image.Save(filename, imgFormat);
            }

            catch (Exception ex)
            {
            }
        }
    }
}
